<?php
/**
 * Reginx App结构生成工具 
 * php init.php -b e:\nginx\html\nfw\user -d ../data
 * $Id: tool.php 1445 2012-08-31 08:25:01Z lan7 $
 */


/**
 * 生成相对路径
 *
 * @param unknown_type $start_dir
 * @param unknown_type $final_dir
 * @return unknown
 */
function getRelativePath($start_dir, $final_dir){
    $start_dir = str_replace('\\','/',$start_dir);
    $final_dir = str_replace('\\','/',$final_dir);
    //
    $firstPathParts  = explode('/', $start_dir);
    $secondPathParts = explode('/', $final_dir);
    //
    $sameCounter = 0;
    for($i = 0; $i < min( count($firstPathParts), count($secondPathParts) ); $i++) {
        if( strtolower($firstPathParts[$i]) !== strtolower($secondPathParts[$i]) ) {
            break;
        }
        $sameCounter++;
    }
    if( $sameCounter == 0 ) {
        return $final_dir;
    }
    //
    $newPath = '';
    for($i = $sameCounter; $i < count($firstPathParts); $i++) {
        if( $i > $sameCounter ) {
            $newPath .= '/';
        }
        $newPath .= "..";
    }
    if( count($newPath) == 0 ) {
        $newPath = ".";
    }
    for($i = $sameCounter; $i < count($secondPathParts); $i++) {
        if($newPath == ''){
            $newPath .= './';
        }else{
            $newPath .= '/';
        }
        $newPath .= $secondPathParts[$i];
    }
    //
    return str_replace('\\','/',$newPath);
}

$hi = <<<e
/**
* Reginx 示例 Module
*
*/
class index_mod extends mod{
    function indexAct(){
        echo('hi,reginx !');
    }
}
e;
$index = <<<s
define('IN_REG'     , true);
define('APP_ID'     , '#APPID#');
define('APP_PATH'   , dirname(__FILE__) . '/');
define('ROOT_PATH'  , dirname(APP_PATH) . '/');
define('REGINX_PATH', realpath('#PATH#'). '/');
define('APP_URL',array_shift(explode('/index.php',strtolower("http://{\$_SERVER['HTTP_HOST']}{\$_SERVER['SCRIPT_NAME']}"))));
include_once(REGINX_PATH . 'reginx.php');
reginx::run(APP_PATH.'conf/config.php');
s;
error_reporting(0);
$argv = $_SERVER['argv'];
$key  = array_search('-b',$argv);
$baseDir = '';
if($key !== FALSE){
    $dir = $baseDir = $argv[$key+1];
    if(substr($baseDir,-1) == '/' || substr($baseDir,-1) == '\\'){
        $dir = $baseDir = substr($baseDir,0,strlen($baseDir));
    }
    if($dir != NULL && $dir != '' && is_dir(dirname($dir))){
        mkdir($dir,0775);
        mkdir($dir.'/conf',0775);
        mkdir($dir.'/include',0775);
        mkdir($dir.'/include/lib',0775);
        mkdir($dir.'/include/mod',0775);
        mkdir($dir.'/include/tab',0775);
        mkdir($dir.'/include/lang',0775);
        mkdir($dir.'/template',0775);
        file_put_contents($dir.'/template/index.html',' ');
        file_put_contents($dir.'/include/index.html',' ');
        file_put_contents($dir.'/include/mod/index.html',' ');
        file_put_contents($dir.'/include/lib/index.html',' ');
        file_put_contents($dir.'/include/tab/index.html',' ');
        $rp = getRelativePath($baseDir,dirname(dirname(__FILE__)));
        // 当前app与reginx为同级目录
        if(realpath(strtolower(dirname(dirname(dirname(__FILE__))))) == realpath(strtolower($baseDir))){
            $index = str_replace('dirname(APP_PATH) . \'/\'','APP_PATH',$index);
            $index = str_replace('#APPID#','',$index);
        }else{
            $index = str_replace('#APPID#',basename($baseDir),$index);
        }
        file_put_contents($dir.'/index.php',"<?php\r\n" . str_replace('#PATH#',$rp,$index) . "\r\n?>");
        file_put_contents($dir.'/conf/config.php',file_get_contents('../conf/config.default.php'));
        file_put_contents($dir.'/include/mod/index.mod.php',"<?php\r\n" . $hi . "\r\n?>");

        $key  = array_search('-d',$argv);
        if($key !== FALSE){
            $dir = $baseDir . '/' . $argv[$key+1];
            if($dir != NULL && $dir != ''){
                mkdir($dir,0775);
                mkdir($dir.'/cache',0755);
                mkdir($dir.'/tpl',0755);
                mkdir($dir.'/log',0755);
                file_put_contents($dir.'/index.html',' ');
                file_put_contents($dir.'/cache/index.html',' ');
                file_put_contents($dir.'/tpl/index.html',' ');
                file_put_contents($dir.'/log/index.html',' ');
            }
        }
        echo("it's ok!\n");
    }else{
        echo("fail \n");
        echo("eg: php init.php -b dir1 -d ../data \n");
        echo("-b app root \n");
        echo("-d data dir \n");
    }
}
?>